package com.example.wan1.utils;

/***
 * 创建时间：2020/7/26 19:58
 * 创建人：10850
 * 功能描述：
 */

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Rect;
import android.net.ConnectivityManager;
import android.os.Build;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.example.wan1.R;
import com.example.wan1.app.MyApplication;
import com.example.wan1.model.UserBean;
import com.google.android.material.tabs.TabLayout;

import java.lang.reflect.Field;
import java.util.Random;

import static android.content.res.Configuration.UI_MODE_NIGHT_NO;
import static android.content.res.Configuration.UI_MODE_NIGHT_UNDEFINED;
import static android.content.res.Configuration.UI_MODE_NIGHT_YES;
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY;
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO;
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES;

/**
 * @author zxd
 * @Description 工具类
 * @Time 2018/12/3 0003 22:04
 */
public class ToolsUtils {
    /**
     * 检查是否有可用网络
     */
    public static boolean isNetworkConnected() {
        ConnectivityManager connectivityManager = (ConnectivityManager) MyApplication.getInstance().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        assert connectivityManager != null;
        return connectivityManager.getActiveNetworkInfo() != null;
    }

    /**
     * 手动反射 设置 tabLayout 下划线宽度 （适配API 28 ）
     *
     * @param tabLayout
     * @param margin
     */
    public static void setIndicatorWidth(@NonNull final TabLayout tabLayout, final int margin) {
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                try {
                    // 拿到tabLayout的slidingTabIndicator属性
                    Field slidingTabIndicatorField = tabLayout.getClass().getDeclaredField("slidingTabIndicator");
                    slidingTabIndicatorField.setAccessible(true);
                    LinearLayout mTabStrip = (LinearLayout) slidingTabIndicatorField.get(tabLayout);
                    for (int i = 0; i < mTabStrip.getChildCount(); i++) {
                        View tabView = mTabStrip.getChildAt(i);
                        //拿到tabView的mTextView属性
                        Field textViewField = tabView.getClass().getDeclaredField("textView");
                        textViewField.setAccessible(true);
                        TextView mTextView = (TextView) textViewField.get(tabView);
                        tabView.setPadding(0, 0, 0, 0);
                        // 因为想要的效果是字多宽线就多宽，所以测量mTextView的宽度
                        int width = mTextView.getWidth();
                        if (width == 0) {
                            mTextView.measure(0, 0);
                            width = mTextView.getMeasuredWidth();
                        }
                        // 设置tab左右间距,注意这里不能使用Padding,因为源码中线的宽度是根据tabView的宽度来设置的
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
                        params.width = width;
                        params.leftMargin = margin;
                        params.rightMargin = margin;
                        tabView.setLayoutParams(params);
                        tabView.invalidate();
                    }
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 隐藏键盘
     *
     * @param etInput
     */
    public static void hideSoftInput(EditText etInput) {
        InputMethodManager imm = (InputMethodManager) etInput.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        assert imm != null;
        imm.hideSoftInputFromWindow(etInput.getWindowToken(), 0); // 强制隐藏键盘
    }

    /**
     * 隐藏 activity 当前的软键盘
     *
     * @param activity
     */
    public static void hideKeyboard(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        //Find the currently focused view, so we can grab the correct window token from it.
        View view = activity.getCurrentFocus();
        //If no view currently has focus, create a new one, just so we can grab a window token from it
        if (view == null) {
            view = new View(activity);
        }
        assert imm != null;
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    /**
     * 显示键盘
     *
     * @param etInput
     */
    public static void showSoftInput(EditText etInput) {
        InputMethodManager imm = (InputMethodManager) etInput.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        assert imm != null;
        etInput.requestFocus();
        imm.showSoftInput(etInput, 0);
    }

    public static void showSoftInput2(EditText etInput) {
        InputMethodManager imm = (InputMethodManager) etInput.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        assert imm != null;
        //获取焦点
        etInput.requestFocus();
        imm.showSoftInput(etInput, InputMethodManager.RESULT_SHOWN);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }

    /**
     * 隐藏或显示软键盘
     * 如果现在是显示调用后则隐藏 反之则显示
     *
     * @param activity
     */
    public static void showORhideSoftKeyboard(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        assert imm != null;
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    /**
     * 判断软键盘是否显示方法
     *
     * @param activity
     * @return
     */

    public static boolean isSoftShowing(Activity activity) {
        //获取当屏幕内容的高度
        int screenHeight = activity.getWindow().getDecorView().getHeight();
        //获取View可见区域的bottom
        Rect rect = new Rect();
        //DecorView即为activity的顶级view
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        //考虑到虚拟导航栏的情况（虚拟导航栏情况下：screenHeight = rect.bottom + 虚拟导航栏高度）
        //选取screenHeight*2/3进行判断
        return screenHeight - rect.bottom - getSoftButtonsBarHeight(activity) != 0;
    }

    /**
     * 底部虚拟按键栏的高度
     *
     * @return
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private static int getSoftButtonsBarHeight(Activity activity) {
        DisplayMetrics metrics = new DisplayMetrics();
        //这个方法获取可能不是真实屏幕的高度
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int usableHeight = metrics.heightPixels;
        //获取当前屏幕的真实高度
        activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
        int realHeight = metrics.heightPixels;
        if (realHeight > usableHeight) {
            return realHeight - usableHeight;
        } else {
            return 0;
        }
    }

    /**
     * 随机获取颜色
     *
     * @return
     */
    public static int getRandColor() {
        Random random = new Random();
        int r = 0;
        int g = 0;
        int b = 0;
        for (int i = 0; i < 2; i++) {
            int temp = random.nextInt(16);
            r = r * 16 + temp;
            temp = random.nextInt(16);
            g = g * 16 + temp;
            temp = random.nextInt(16);
            b = b * 16 + temp;
        }
        return Color.rgb(r, g, b);
    }

    /**
     * 随机从固定的颜色组中获取颜色
     *
     * @return
     */
    public static int getRandSomeColor(Context context) {
        Random random = new Random();
        int[] color = {R.color.colorPrimary,
                android.R.color.holo_green_light,
                android.R.color.holo_red_light,
                android.R.color.holo_blue_light,
                android.R.color.holo_orange_light};
        int temp = random.nextInt(5);
        return ContextCompat.getColor(context, color[temp]);
    }

    public static String getVersion(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            return info.versionName;
        } catch (Exception e) {
            e.printStackTrace();
            return "null";
        }
    }

    /**
     * 根据积分获取等级
     *
     * @param coin
     * @return
     */
    public static int getRank(int coin) {
        int mCoin = coin;
        if (mCoin > 100) {
            if (coin % 100 == 0) {
                //整数等级不增加
                mCoin = (coin - (coin % 100)) / 100;
            } else {
                mCoin = (coin - (coin % 100)) / 100 + 1;
            }
        } else {
            //100积分以下
            mCoin = 1;
        }
        return mCoin;
    }

    /***
     * 获取当前主题style的属性值
     */
    public static int getAttrValue(Context context) {
        TypedValue typedValue = new TypedValue();
        context.getTheme().resolveAttribute(R.attr.colorPrimary,
                typedValue, true);
        return typedValue.data;
    }

    /***
     * 获取当前主题style的属性id
     */
    public static int getAttrId(Context context) {
        TypedValue typedValue = new TypedValue();
        context.getTheme().resolveAttribute(R.attr.colorPrimary,
                typedValue, true);
        return typedValue.resourceId;
    }

    /***
     * 获取主题color的颜色，如#000000
     */
    public static int getThemeColor(Context context) {
        String theme_color = (String) SPUtils.get(context, "theme_color", "");
        if (!TextUtils.isEmpty(theme_color))
            return Color.parseColor(theme_color);
        else
            return 0;
    }

    public static UserBean getUser(Context context) {
        String info = (String) SPUtils.get(context, "user_info", "");
        if (!TextUtils.isEmpty(info)) {
            String[] strings = info.split("-");
            UserBean userBean = new UserBean();
            userBean.setUsername(strings[0]);
            userBean.setPassword(strings[1]);
            userBean.setRank(Integer.valueOf(strings[2]));
            userBean.setCoinCount(Integer.valueOf(strings[3]));
            userBean.setLevel(Integer.valueOf(strings[4]));
            return userBean;
        }
        return null;
    }

    public static boolean isOpenNavigationBar(Context context) {
        return (boolean) SPUtils.get(context, "navigation_bar_color", false);
    }

    /***
     * 获取黑夜模式
     * @param context
     * @return
     */
    public static boolean getNightMode(Context context) {
        int i = (int) SPUtils.get(context, "night_mode_id", 1);
        if (i == MODE_NIGHT_YES) {
            return true;
        } else if (i == MODE_NIGHT_NO) {
            return false;
        } else if (i == MODE_NIGHT_AUTO_BATTERY) {
            return true;
        } else if (i == MODE_NIGHT_FOLLOW_SYSTEM) {
            int currentNightMode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
            switch (currentNightMode) {
                case UI_MODE_NIGHT_YES:
                    return true;
                case UI_MODE_NIGHT_NO:
                case UI_MODE_NIGHT_UNDEFINED:
                    return false;
            }
            return true;
        }
        return false;
    }

    public static int getColor(Context context) {
        return ToolsUtils.getNightMode(context) ? Color.parseColor("#3E3E3F") :
                ToolsUtils.getThemeColor(context) != 0 ? ToolsUtils.getThemeColor(context) :
                        ToolsUtils.getAttrValue(context);
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     *
     * @param dpValue 虚拟像素
     * @return 像素
     */
    //<editor-fold desc="像素密度">
    private static float density = Resources.getSystem().getDisplayMetrics().density;

    public static int dp2px(float dpValue) {
        return (int) (0.5f + dpValue * density);
    }
}

